//
//  PrivacProtectViewController.m
//  LocomotiveGuards
//
//  Created by zwj on 15-11-30.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "PrivacProtectViewController.h"
#import "PopLoadMsg.h"
#import "PopMsg.h"
#import "LoginViewController.h"
#import "AlertView.h"

@interface PrivacProtectViewController ()
{
    NSDictionary *_listDic;
    NSInteger _immedFlag;
    NSInteger _anchorFlag;
    NSInteger _trackFlag;
}

typedef enum {
    ImmediatelyType = 0,
    AnchorType,
    TrackType
}Type;

@property (nonatomic, strong) NSString *userOrTourist;

@end

@implementation PrivacProtectViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.userOrTourist = [[NSUserDefaults standardUserDefaults] objectForKey:@"userOrTourist"];
    
    [self createMyNav];
    [self configView];
    [self getPrivacyRequest];
}

- (void)createMyNav
{
    UIImageView *imgView = [self addTitle:@"隐私保护" imageName:@"privacprotection"];
    imgView.frame = CGRectMake((SCREEN_WIDTH-(15+5+70)*W)/2, 20+19*H, 15*W, 10*H);
    [self addBackBtnTarget:self action:@selector(backBtnClick)];
}

- (void)configView
{
    NSArray *imgArray = @[@"immediately_icon",@"anchor_icon",@"track_icon"];
    NSArray *titleArray = @[@"即时定位",@"锚点定位",@"轨迹定位"];
    for (int i=0; i<3; i++) {
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 20+44*H+55*H*i, SCREEN_WIDTH, 55*H-1)];
        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30*W, 20*H, 15*H, 15*H)];
        [leftImageView setImage:[UIImage imageNamed:imgArray[i]]];
        [bgView addSubview:leftImageView];
        
        UIView *separateView = [[UIView alloc] initWithFrame:CGRectMake(0, 20+44*H+55*H*(i+1)-1, SCREEN_WIDTH, 1)];
        separateView.backgroundColor = [UIColor colorWithRed:245.f/255.f green:245.f/255.f blue:245.f/255.f alpha:1.0f];
        [self.view addSubview:separateView];
        
        UILabel *lacateLabel = [self createLabelFrame:CGRectMake(40*W+CGRectGetWidth(leftImageView.frame), 13*H, 70*W, 30*H) title:titleArray[i] font:[UIFont systemFontOfSize:16]];
        lacateLabel.textColor = [UIColor grayColor];
        lacateLabel.textAlignment = NSTextAlignmentLeft;
        [bgView addSubview:lacateLabel];
        
        UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-100*W, 15*H, 50*W, 25*H)];
        [rightImageView setImage:[UIImage imageNamed:@"off"]];
        rightImageView.tag = 10+i;
        rightImageView.userInteractionEnabled = YES;
        UIImageView *slipImageView = [[UIImageView alloc] initWithFrame:CGRectMake(3*W, 2*H, CGRectGetHeight(rightImageView.frame)-4*H, CGRectGetHeight(rightImageView.frame)-4*H)];
        [slipImageView setImage:[UIImage imageNamed:@"slip"]];
        slipImageView.tag = 100+i;
        [rightImageView addSubview:slipImageView];
        
        UIButton *slipBtn = [self createBtnFrame:CGRectMake(0, 0, 50*W, 25*H) title:nil bgImageName:nil selectedImageName:nil target:self action:@selector(slip:)];
        slipBtn.tag = 1000+i;
        [rightImageView addSubview:slipBtn];
        
        [bgView addSubview:rightImageView];
        [self.view addSubview:bgView];
    }
    
    UILabel *tipLabel = [self createLabelFrame:CGRectMake(30*W, 20+44*H+55*H*3, SCREEN_WIDTH-60*W, 60*H) title:@"轨迹开关若处于关闭状态时，服务器将不能获取您的车辆轨迹。若触发报案状态，系统将自动打开定位开关，便于破案。" font:[UIFont systemFontOfSize:13]];
    tipLabel.textColor = [UIColor grayColor];
    tipLabel.numberOfLines = 0;
    tipLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:tipLabel];
}

- (void)reloadView
{
    if (!_listDic) {
        return;
    }
    _immedFlag = [[_listDic objectForKey:@"rtFlag"] integerValue];
    _anchorFlag = [[_listDic objectForKey:@"anchorFlag"] integerValue];
    _trackFlag = [[_listDic objectForKey:@"trackFlag"] integerValue];
    [self setSlipWithType:ImmediatelyType andFlag:_immedFlag];
    [self setSlipWithType:AnchorType andFlag:_anchorFlag];
    [self setSlipWithType:TrackType andFlag:_trackFlag];
}

- (void)setSlipWithType:(Type)index andFlag:(NSInteger)flag
{
    UIImageView *rightImg = (UIImageView *)[self.view viewWithTag:10+index];
    UIImageView *slipImg = (UIImageView *)[self.view viewWithTag:100+index];
    if (flag == 0) {
        [rightImg setImage:[UIImage imageNamed:@"on"]];
        slipImg.frame = CGRectMake(CGRectGetWidth(rightImg.frame)-21*H-3*W, 2*H, 21*H, 21*H);
    }
    else {
        [rightImg setImage:[UIImage imageNamed:@"off"]];
        slipImg.frame = CGRectMake(3*W, 2*H, 21*H, 21*H);
    }
}

#pragma mark - 网络请求
//获取隐私信息
- (void)getPrivacyRequest
{
    PopLoadMsg *loadmsg = [PopLoadMsg new];
    [loadmsg delayLoadImages];
    
    NSDictionary *paramdic = @{@"bid":[[NSUserDefaults standardUserDefaults] objectForKey:@"bid"]};
    NSString *url = [urlstring stringByAppendingString:@"getPrivacyGuardConfig.do"];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:url withParam:paramdic success:^(AFHTTPRequestOperation *op,id responseObject){
        [loadmsg delayImagesRemove];
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSLog(@"dic = %@", dic);
        NSInteger flag = [[dic objectForKey:@"flag"] integerValue];
        if (flag == 0) {
            _listDic = dic;
            [self reloadView];
        }

    }failure:^(AFHTTPRequestOperation *op,NSError *error){
        [loadmsg delayImagesRemove];
        [PopMsg showMessage:@"网络异常，请重试" Lasttime:1.0f];
    }];
}

//设置隐私信息
- (void)setPrivacyRequest
{
    PopLoadMsg *loadmsg = [PopLoadMsg new];
    [loadmsg delayLoadImages];
    
    NSDictionary *paramdic = @{@"bid":[[NSUserDefaults standardUserDefaults] objectForKey:@"bid"],
                               @"rtFlag":[NSNumber numberWithInteger:_immedFlag],
                               @"anchorFlag":[NSNumber numberWithInteger:_anchorFlag],
                               @"trackFlag":[NSNumber numberWithInteger:_trackFlag]};

    NSString *url = [urlstring stringByAppendingString:@"setPrivacyGuardConfig.do"];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:url withParam:paramdic success:^(AFHTTPRequestOperation *op,id responseObject){
        [loadmsg delayImagesRemove];
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSLog(@"dic = %@", dic);
        NSInteger flag = [[dic objectForKey:@"flag"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        if (msg) {
            [PopMsg showMessage:msg Lasttime:1.0f];
        }
        if (flag == 0) {
            [self setSlipWithType:ImmediatelyType andFlag:_immedFlag];
            [self setSlipWithType:AnchorType andFlag:_anchorFlag];
            [self setSlipWithType:TrackType andFlag:_trackFlag];
        }
        
    }failure:^(AFHTTPRequestOperation *op,NSError *error){
        [loadmsg delayImagesRemove];
        [PopMsg showMessage:@"网络异常，请重试" Lasttime:1.0f];
    }];
}

#pragma mark - 点击事件
- (void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

//点击切换开关，设置隐私
- (void)slip:(UIButton *)btn
{
    if ([self.userOrTourist isEqualToString:@"1"]) {
        if (btn.tag-1000 == ImmediatelyType) {
            if (_immedFlag == 0) {
                _immedFlag = 1;
            }
            else if (_immedFlag == 1) {
                _immedFlag = 0;
            }
        }
        else if (btn.tag-1000 == AnchorType) {
            if (_anchorFlag == 0) {
                _anchorFlag = 1;
            }
            else if (_anchorFlag == 1) {
                _anchorFlag = 0;
            }
        }
        else if (btn.tag-1000 == TrackType) {
            if (_trackFlag == 0) {
                _trackFlag = 1;
            }
            else if (_trackFlag == 1) {
                _trackFlag = 0;
            }
        }
        
        //操作成功后切换界面按钮状态
        [self setPrivacyRequest];
    }
    else {
        AlertView *alertView = [[AlertView alloc] initWithConfirmButtonBlock:^{
            NSLog(@"登录");
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            [self presentViewController:loginVC animated:YES completion:nil];
        } cancelButtonBlock:^{
            NSLog(@"取消");
        }];
        [self.view addSubview:alertView];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
